
package org.opentripplanner.nedimporter;

import java.io.File;
import java.io.IOException;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffFormat;
import org.geotools.gce.geotiff.GeoTiffReader;

/**
 * An implementation of the NEDCoverageSource for the GeoTIFF format.
 *
 * @author demory
 */
public class GeotiffCoverageSource implements NEDCoverageSource {
  
  private File tiffFile_;
  private GridCoverage2D coverage_;

  public GeotiffCoverageSource(String filename) {
    this(new File(filename));
  }

  public GeotiffCoverageSource(File tiffFile) {
    tiffFile_ = tiffFile;
    readFile();
  }

  public boolean readFile() {
    GeoTiffFormat format = new GeoTiffFormat();
    GeoTiffReader reader = null;

    try {
      reader = (GeoTiffReader) format.getReader(tiffFile_);
    } catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }

    try {
      System.out.println("loading coverage");
      coverage_ = (GridCoverage2D) reader.read(null);
    } catch(IOException ex) {
      ex.printStackTrace();
      return false;
    }

    return true;
  }

  public GridCoverage2D getCoverage() {
    return coverage_;
  }


}
